<{include file="common/header.tpl"}>
<script type="text/javascript">

function fmoney(s, n)  
{  
       n = n > 0 && n <= 20 ? n : 2;  
       s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";  
       var l = s.split(".")[0].split("").reverse(),  
       r = s.split(".")[1];  
       t = "";  
       for(i = 0; i < l.length; i ++ )  
       {  
          t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
       }  
       return t.split("").reverse().join("") + "." + r;  
}  


function confirm_submit(item)
{
    var tr = $(item).parent().parent().parent().parent().parent();
    var tds = tr.find('td');
    var counts  = $(tds[5]).text();
    var rcounts = $(tds[6]).text();
    var ratio   = $(tds[7]).text();
    var money   = $(tds[8]).text();

    var str = "预算详情：\n" + 
              "原始效果数：" + rcounts + "\n" + 
              "调整系数：" + ratio + "\n" + 
              "调整后效果数：" + counts + "\n" + 
              "调整后金额数：" + money  + "\n" +
              "确认这笔预算吗？";

    return window.confirm(str);
}

function show_editor(divitem)
{
    var editor = $(divitem).parent().next();
    if (editor.is(':hidden'))
        editor.show();
    else
        editor.hide();

    var btnconfirm = $(divitem).parent();
    if (editor.is(':hidden'))
        btnconfirm.show();
    else
        btnconfirm.hide();
    return false;
}

function close_editor (divitem)
{
    var editor = $(divitem).parent().parent().parent();
    editor.hide();
    var btnconfirm = $(divitem).parent().parent().parent().prev();
    btnconfirm.show();

    var tr = $(divitem).parent().parent().parent().parent().parent();
    var tds = tr.find('td');
    var counts  = $(tds[5]).attr('val');
    var ratio   = $(tds[7]).attr('val');
    var money   = $(tds[8]).attr('val');

    $(tds[5]).html(counts);
    $(tds[7]).html(ratio + '%');
    $(tds[8]).html('￥'+money);

    var ratio_input = tr.find('form input[name=ratio]');
    ratio_input.val(ratio);
}

function change_ratio (item)
{
    var tr = $(item).parent().parent().parent().parent().parent();
    var tds = tr.find('td');
    var counts  = parseInt ($(tds[5]).attr('val'), 10);
    var rcounts = parseInt ($(tds[6]).attr('val'), 10);
    var ratio1  = parseInt ($(tds[7]).attr('val'), 10);
    var money   = $(tds[8]).attr('val');
    money       = parseInt (money.replace(',', ''), 10);

    var ratio2  = parseInt ($(item).val(), 10);
    var counts2 = Math.round (rcounts * ratio2 / 100);
    var money2  = Math.round (counts2 / counts * money);
    money2      = fmoney(money2, 2);
    $(tds[5]).html('<b class="alter">'+counts2+'</b>');
    $(tds[7]).html('<b class="alter">'+ratio2+'</b>%');
    $(tds[8]).html('￥<b class="alter">'+money2+'</b>');
}
</script>

<section id="spread_one" class="column">
        <h4 class="alert_info">欢迎使用 哇吖管理系统！</h4>
        <article class="module width_3_quarter">
        <header>
            <h3 class="tabs_involved " style="width:auto">待审核的预算帐目 (<{$budgets.count}>笔)</h3>
        </header>
		<div class="tab_container">
			<div id="tab1" class="tab_content">
			<table class="tablesorter" cellspacing="0"> 
			<thead> 
				<tr> 
    				<th>预算月份</th> 
    				<th>活动名称</th> 
    				<th>渠道名称</th> 
    				<th>渠道标识</th> 
    				<th>计费方式</th> 
    				<th>推广数据</th> 
					<{if $role_id == 'commonmanager' || $role_id=='supermanager'}>
    				<th>实际数据</th> 
    				<th>系数</th> 
					<{/if}>
    				<th>收入</th>
    				<th>状态</th>
    				<th>操作</th>
				</tr> 
			</thead> 
			<tbody>
				<{foreach from =$budgets.list item = 'v'}> 
					<tr>
						<td><{$v.no}></td>
						<td><{$v.activity_info.name}></td>
						<td><{$v.channel_info.name}></td>
						<td><{$v.channel_info.unique_key}></td>
						<td><{$v.activity_info.pay_type}></td>
						<td val="<{$v.counts}>"><{$v.counts}></td>
						<{if $role_id == 'commonmanager' || $role_id == 'supermanager'}>
						<td val="<{$v.orig_counts}>"><{$v.orig_counts}></td>
						<td val="<{$v.ratio}>"><{$v.ratio}>%</td>
						<{/if}>
						<td val="<{$v.money}>">￥<{$v.money}></td>
						<td><{$v.status_name}></td>
						<td>
                            <{if ($v.status == 0 && $user_info.role_id == 'auditor') || ($v.status == 1 && ($user_info.role_id == 'commonmanager' || $user_info.role_id=='supermanager')) }>
                            <span class="btn-confirm">
                                <a href="/budget/verify/<{$v.id}>">确认</a>
                                <{if $v.status == 1 && $user_info.role_id == 'commonmanager' }>
                                |
                                <a href="javascript:void(0)" onclick="return show_editor(this)">调整</a> 
                                <{/if}>
                            </span>

                                <{if $v.status == 1 && ($user_info.role_id == 'commonmanager' || $user_info.role_id=='supermanager')}>
                            <div class="hidden">
                                <form method="POST" action="/budget/verify/<{$v.id}>">
                                <div>
                                    系数:<input type="text" name="ratio" value="<{$v.ratio}>" style="width:30px;" onchange="change_ratio(this)">%
                                    <input type="submit" name="submit_ratio" value="确认" onclick="return confirm_submit(this)">
                                    <input type="button" name="cancel" value="关闭" onclick="return close_editor(this)">
                                </div>
                                </form> 
                            </div>
                                <{/if}>
                            <{else}>
                            --
                            <{/if}>
                        </td>
					</tr> 
				<{/foreach}>
			</tbody> 
			</table>
			</div><!-- end of #tab1 -->
		</div><!-- end of .tab_container -->
		</article><!-- end of content manager article -->
		<div class="clear"></div>
	</section>			
<{include file="common/footer.tpl"}>
